

#include <stdint.h>

//
// Name:    CRC-8               x8+x2+x+1
// Note:    d for 直接计算
//
//__attribute__((section(".func.data"))) const char *msg = "CRC8D";
//__attribute__((section(".func.data"))) unsigned char crc = 0xa5; // Initial value
__attribute__((section(".func.text"))) unsigned int crc8_d(unsigned int data, unsigned int length)
{
    //asm volatile("ebreak");
    const unsigned char *ptr = (const unsigned char *)data;
    volatile unsigned int len = length;
    unsigned char crc = 0xa5;
    while (len--)
    {
        crc ^= *ptr++; // crc ^= *data; data++;
        for (volatile unsigned int i = 0; i < 8; i++)
        {
            if (crc & 0x80)
                crc = (crc << 1) ^ 0x07;
            else
                crc <<= 1;
        }
    }

    //asm volatile("ebreak");
    return crc;
}
